package com.baymax.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baymax.entity.dto.R;
import com.baymax.entity.dto.UserDTO;
import com.baymax.entity.vo.UserVO;
import com.baymax.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 用户 控制层
 *
 * @author baymax
 * @date 2023-03-17 14:54
 */
@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping(value = "/user")
public class UserController {

    private final UserService userService;

    /**
     * 分页查询用户信息
     */
    @GetMapping
    public R<IPage<UserVO>> page(UserDTO userDTO, Page page) {
        return R.ok(userService.pageUser(userDTO, page));
    }

    /**
     * 查询用户详情
     */
    @GetMapping(value = "detail")
    public R<UserVO> detail(@RequestParam("userId") Integer userId, HttpServletRequest request) {
        return R.ok(userService.detail(userId));
    }

    /**
     * 新增用户
     */
    @PostMapping
    public R<Boolean> add(UserDTO userDTO) {
        return R.ok(userService.addUserInfo(userDTO));
    }

    /**
     * 编辑用户
     */
    @PutMapping
    public R<Boolean> edit(UserDTO userDTO) {
        return R.ok(userService.editUserInfo(userDTO));
    }

    /**
     * 删除用户
     */
    @DeleteMapping
    public R<Boolean> delete(Integer[] userIds) {
        return R.ok(userService.delete(Arrays.asList(userIds)));
    }

    /**
     * 撤销
     */
    @PostMapping(value = "/revocation")
    public R<Boolean> revocation() {
        return userService.revocation() ? R.ok() : R.fail();
    }

}
